﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GAMS.APP.App_UI.Commons;
using KPIS.GERP.GAMS.DAL;
using KPIS.GERP.GAMS.MODEL;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class Login : System.Web.UI.Page
    {
        Main main = new Main();
        D_SECURITY_Users user = new D_SECURITY_Users();

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Session["UserName"] != null)
            //{
            //    ((Label)((LinkButton)((RadMenu)Master.FindControl("RadMenu1")).Items[9].FindControl("cmdLogout")).FindControl("lblUserName")).Text = Session["UserName"].ToString();
            //}
        }

        protected void cmdLogin_Click(object sender, EventArgs e)
        {
            string strUserID = txtUserID.Text.ToUpper().Trim();
            string strPassword = txtPassword.Text.Trim();

            //============= Test Area ==============
            /*if (strUserID.Length == 0)
            {
                strUserID = "70";
                strPassword = "kpis";
            }*/
            //======================================

            if (strUserID.Length == 0)
            {
                lblError.Text = "กรุณากรอก [รหัสประจำตัว]";
                return;
            }

            if (!CheckPassword(strUserID, strPassword))
            {
                return;
            }

            Response.Redirect(@"~\App_UI\Forms\Default.aspx");
        }

        private bool CheckPassword(string strUserID, string strPassword)
        {
            bool bOK = false;
            DataTable dtb = user.GetUserInfo(strUserID, strPassword);
            string strCorrectPassword = "";

            if (dtb.Rows.Count == 0)
            {
                lblError.Text = "ไม่พบ [รหัสประจำตัว] นี้";
                return bOK;
            }
            else
            {
                strCorrectPassword = dtb.Rows[0]["user_password"].ToString();
            }

            if (strPassword == strCorrectPassword)
            {
                Session["EmpSeq"] = dtb.Rows[0]["emp_seq"].ToString();
                Session["UserJobsiteSeq"] = dtb.Rows[0]["working_job_site_seq"].ToString();
                Session["PosName"] = dtb.Rows[0]["POS_NAME"] != null ? dtb.Rows[0]["POS_NAME"].ToString():string.Empty;
                //Session["SystemJobsiteUpperSeq"] = "2898";

                Session["UserInternalJobsiteSeq"] = dtb.Rows[0]["working_job_site_seq"].ToString();
                //Session["SystemInternalJobsiteSeq"] = "1300902983";
                //Session["SystemInternalJobsiteUpperSeq"] = "2898";
                Session["UserName"] = dtb.Rows[0]["user_name"].ToString();
                Session["UserWorkAreaSeq"] = dtb.Rows[0]["work_area_seq"].ToString();
                Session["Height"] = hHeight.Value;
                bOK = true;
            }
            else
            {
                lblError.Text = "[รหัสผ่าน] ไม่ถูกต้อง";
            }

            return bOK;
        }
    }
}
